package com.lh.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lh.entity.enums.AreaField;
import com.lh.entity.table.ResponsiblePersons;
import com.lh.mapper.ResponsiblePersonsMapper;
import com.lh.service.ResponsiblePersonsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class ResponsiblePersonsServiceImpl implements ResponsiblePersonsService {

    @Resource
    private ResponsiblePersonsMapper responsiblePersonsMapper;

    @Override
    public String locatePerson(String processStep, String areaKey) {
        if (StrUtil.isBlank(processStep) || StrUtil.isBlank(areaKey)) {
            return null;
        }
        char c = areaKey.charAt(0);
        String modifiedAreaKey = areaKey;
        if (c == '市') {
            modifiedAreaKey = "市本级";
        }
        AreaField area = AreaField.from(modifiedAreaKey);
        if (area == null) {
            return null;
        }


        ResponsiblePersons row = responsiblePersonsMapper.selectOne(
                Wrappers.<ResponsiblePersons>lambdaQuery()
                        .like(ResponsiblePersons::getProcessStep, processStep)
                        .last("LIMIT 1")
        );
        if (row == null) return null;

        String v = area.getGetter().apply(row);
        return StrUtil.emptyToNull(StrUtil.trimToEmpty(v));
    }
}
